package com.example.administrator.music;

import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;

import cn.bmob.v3.Bmob;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.UpdateListener;

/**
 * Created by Administrator on 2018/4/3.
 */

public class MenuActivity extends AppCompatActivity implements View.OnClickListener {
    private TextView iconname, theme, about, exit, set,reusername;
    private LinearLayout menglong, bingxue, caise, huaduo, qunshang;
    public static boolean zhuc=false;
    private RelativeLayout menubg;
    private CircleImageView iconimage;
    private LinearLayout boy,cat,girl,lufei,majcain,snowman;



    public static final int THEME_MENGLONG = 101;
    public static final int THEME_BINGXUE = 102;
    public static final int THEME_CAISE = 103;
    public static final int THEME_HUADUO = 104;
    public static final int THEME_QUNSHANG = 105;

    private int sendtheme = 100;

    public static final String BROADCAST_MENUACTIVITY_CONTROL = "MenuActivity.ACTION_CONTROL";
    public static final String BROADCAST_MENUMODE_CONTROL = "MenuActivity.MODE_CONTROL";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.menu);
        initview();

    }

    @Override
    protected void onResume() {
        super.onResume();
        Intent getzc = getIntent();
        zhuc = getzc.getBooleanExtra("Zhuc", false);
        if (zhuc) {
            iconname.setText(load("musicusername") + "已登录");
            exit.setText("退出");
            Toast.makeText(MenuActivity.this, "已登录", Toast.LENGTH_SHORT).show();
        }
        String img=MainActivity.iconimg;
        seticonImg(img);
        setmbg(MainActivity.theme);

    }

    @Override
    protected void onRestart() {
        super.onRestart();
        if(zhuc) {
            iconname.setText(load("musicusername")+"已登录");
            exit.setText("退出");
            Toast.makeText(MenuActivity.this, "已登录", Toast.LENGTH_SHORT).show();
        }
    }

    private void initview() {

        iconimage= (CircleImageView) findViewById(R.id.iconimage);
        iconname = (TextView) findViewById(R.id.iconname);
        theme = (TextView) findViewById(R.id.theme);
        about = (TextView) findViewById(R.id.about);
        exit = (TextView) findViewById(R.id.exid);
        set = (TextView) findViewById(R.id.set);
        menubg= (RelativeLayout) findViewById(R.id.menubg);
        reusername= (TextView) findViewById(R.id.userNP);


        iconname.setOnClickListener(this);
        theme.setOnClickListener(this);
        about.setOnClickListener(this);
        exit.setOnClickListener(this);
        set.setOnClickListener(this);
        reusername.setOnClickListener(this);
        iconimage.setOnClickListener(this);

    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.about:
                AlertDialog.Builder ab=new AlertDialog.Builder(MenuActivity.this);
                ab.setTitle("简介");
                ab.setMessage("简介：这是一个音乐播放器的例子，除了实现基本的音乐播放功能，" +
                        "还实现了后台音乐播放，切换主题，进度条，歌曲搜索，歌词下载和登陆等功能。" +
                        "欢迎提出程序BUG，感谢您的支持。");
                ab.show();
                break;
            case R.id.exid:
                if(zhuc){
                    AlertDialog.Builder builder=new AlertDialog.Builder(MenuActivity.this);
                    builder.setTitle("退出");
                    builder.setMessage("确定要退出登录吗？");
                    builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            MenuActivity.zhuc=false;
                            LoginActivity.zhuc=false;
                            iconname.setText("我的账户(未登录)");
                            exit.setText("登录");
                            File file1 = new File("/data/data/com.example.administrator.music/files/musicusername");
                            File file2 = new File("/data/data/com.example.administrator.music/files/musicuserobjectId");
                            if(file1.exists()){
                                file1.delete();
                            }
                            if(file2.exists()){
                                file2.delete();
                            }
                        }
                    });
                    builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {

                        }
                    });
                    builder.show();
                }else {
                    Intent inten1 = new Intent();
                    inten1.setClass(MenuActivity.this, LoginActivity.class);
                    startActivity(inten1);
                }
                break;
            case R.id.theme:
                AlertDialog.Builder abder = new AlertDialog.Builder(MenuActivity.this);
                LayoutInflater inflater = getLayoutInflater();
                View layout = inflater.inflate(R.layout.theme, null);
                abder.setView(layout);
                abder.setTitle("请选择主题");
                abder.setPositiveButton("确认", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        if (sendtheme == 100) {
                            Toast.makeText(MenuActivity.this, "未选择，设置失败", Toast.LENGTH_SHORT).show();
                        } else {
                            setmbg(sendtheme);
                            File file = new File("/data/data/com.example.administrator.music/files/musicuserobjectId");
                            if(file.exists() && zhuc){
                                MusicPlayUser user=new MusicPlayUser();
                                String id=load("musicuserobjectId").trim();
                                user.setTheme(sendtheme);
                                user.setMode(MainActivity.playmode);
                                user.update(id, new UpdateListener() {
                                    @Override
                                    public void done(BmobException e) {
                                        if (e==null){
                                            sendBroadcastOnTheme(sendtheme);
                                            Toast.makeText(MenuActivity.this, "设置成功", Toast.LENGTH_SHORT).show();
                                        }else {
                                            sendBroadcastOnTheme(sendtheme);
                                            Toast.makeText(MenuActivity.this, "设置成功，但同步失败"+e, Toast.LENGTH_SHORT).show();
                                        }
                                    }
                                });
                            }else {
                                sendBroadcastOnTheme(sendtheme);
                                Toast.makeText(MenuActivity.this, "设置成功", Toast.LENGTH_SHORT).show();
                            }
                        }
                    }
                });
                abder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                });
                menglong = (LinearLayout) layout.findViewById(R.id.menglong);
                bingxue = (LinearLayout) layout.findViewById(R.id.bingxue);
                caise = (LinearLayout) layout.findViewById(R.id.caishe);
                huaduo = (LinearLayout) layout.findViewById(R.id.huaduo);
                qunshang = (LinearLayout) layout.findViewById(R.id.qunshang);

                menglong.setOnClickListener(this);
                bingxue.setOnClickListener(this);
                caise.setOnClickListener(this);
                huaduo.setOnClickListener(this);
                qunshang.setOnClickListener(this);
                abder.show();

                break;
            case R.id.menglong:
                setBg();
                menglong.setBackgroundColor(getResources().getColor(R.color.pressed));
                sendtheme = THEME_MENGLONG;
                break;
            case R.id.bingxue:
                setBg();
                bingxue.setBackgroundColor(getResources().getColor(R.color.pressed));
                sendtheme = THEME_BINGXUE;
                break;
            case R.id.caishe:
                setBg();
                caise.setBackgroundColor(getResources().getColor(R.color.pressed));
                sendtheme = THEME_CAISE;
                break;
            case R.id.huaduo:
                setBg();
                huaduo.setBackgroundColor(getResources().getColor(R.color.pressed));
                sendtheme = THEME_HUADUO;
                break;
            case R.id.qunshang:
                setBg();
                qunshang.setBackgroundColor(getResources().getColor(R.color.pressed));
                sendtheme = THEME_QUNSHANG;
                break;
            case R.id.set:
                AlertDialog.Builder builder=new AlertDialog.Builder(MenuActivity.this);
                builder.setTitle("歌曲播放模式");
                builder.setItems(R.array.theme, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        switch (which){
                            case 0:
                                sendBroadcastOnmode(201);
                                setmode(201);
                                break;
                            case 1:
                                sendBroadcastOnmode(202);
                                setmode(202);
                                break;
                            case 2:
                                sendBroadcastOnmode(203);
                                setmode(203);
                                break;
                            case 3:
                                sendBroadcastOnmode(204);
                                setmode(204);
                                break;
                        }
                    }
                });
                builder.show();
                break;
            case R.id.userNP:
                File file = new File("/data/data/com.example.administrator.music/files/musicuserobjectId");
                if (file.exists() && zhuc){
                    Intent in1=new Intent();
                    String oldname= String.valueOf(iconname.getText());
                    in1.putExtra("oldname",oldname);
                    in1.setClass(MenuActivity.this,ChangeUsername.class);
                    startActivity(in1);
                }else {
                    Toast.makeText(MenuActivity.this,"尚未登陆账号，请前往登陆",Toast.LENGTH_SHORT).show();
                }
                break;
            case R.id.iconimage:
                AlertDialog.Builder abder1 = new AlertDialog.Builder(MenuActivity.this);
                LayoutInflater inflater1 = getLayoutInflater();
                View layout1 = inflater1.inflate(R.layout.iconimage, null);
                abder1.setView(layout1);
                abder1.setTitle("请选择头像");
                abder1.setPositiveButton("确认", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        seticonImg(MainActivity.iconimg);
                        File file = new File("/data/data/com.example.administrator.music/files/musicuserobjectId");
                        if(file.exists() && zhuc){
                            MusicPlayUser user=new MusicPlayUser();
                            String id=load("musicuserobjectId").trim();
                            user.setTheme(sendtheme);
                            user.setMode(MainActivity.playmode);
                            user.setIconimage(MainActivity.iconimg);
                            user.update(id, new UpdateListener() {
                                @Override
                                public void done(BmobException e) {
                                    if (e==null){
                                        Toast.makeText(MenuActivity.this, "设置成功", Toast.LENGTH_SHORT).show();
                                    }else {
                                        Toast.makeText(MenuActivity.this, "设置成功，但同步失败"+e, Toast.LENGTH_SHORT).show();
                                    }
                                }
                            });
                        }else {
                            Toast.makeText(MenuActivity.this, "设置成功", Toast.LENGTH_SHORT).show();
                        }

                    }
                });
                abder1.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                });
                abder1.show();
                boy= (LinearLayout) layout1.findViewById(R.id.iconimage_boy);
                cat= (LinearLayout) layout1.findViewById(R.id.iconimage_cat);
                girl= (LinearLayout) layout1.findViewById(R.id.iconimage_girl);
                lufei= (LinearLayout) layout1.findViewById(R.id.iconimage_lufei);
                majcain= (LinearLayout) layout1.findViewById(R.id.iconimage_majcain);
                snowman= (LinearLayout) layout1.findViewById(R.id.iconimage_snowman);

                boy.setOnClickListener(this);
                cat.setOnClickListener(this);
                girl.setOnClickListener(this);
                lufei.setOnClickListener(this);
                majcain.setOnClickListener(this);
                snowman.setOnClickListener(this);
                break;
            case R.id.iconimage_boy:
                seticonBg();
                boy.setBackgroundColor(getResources().getColor(R.color.pressed));
                MainActivity.iconimg="10";
                break;
            case R.id.iconimage_cat:
                seticonBg();
                cat.setBackgroundColor(getResources().getColor(R.color.pressed));
                MainActivity.iconimg="11";
                break;
            case R.id.iconimage_girl:
                seticonBg();
                girl.setBackgroundColor(getResources().getColor(R.color.pressed));
                MainActivity.iconimg="12";
                break;
            case R.id.iconimage_lufei:
                seticonBg();
                lufei.setBackgroundColor(getResources().getColor(R.color.pressed));
                MainActivity.iconimg="13";
                break;
            case R.id.iconimage_majcain:
                seticonBg();
                majcain.setBackgroundColor(getResources().getColor(R.color.pressed));
                MainActivity.iconimg="14";
                break;
            case R.id.iconimage_snowman:
                seticonBg();
                snowman.setBackgroundColor(getResources().getColor(R.color.pressed));
                MainActivity.iconimg="15";
                break;
        }

    }
    private void setmode(int mode){
        File file = new File("/data/data/com.example.administrator.music/files/musicuserobjectId");
        if (file.exists() && zhuc){
            MusicPlayUser musicPlayUser=new MusicPlayUser();
            String uid=load("musicuserobjectId").trim();
            musicPlayUser.setMode(mode);
            musicPlayUser.setTheme(MainActivity.theme);
            musicPlayUser.update(uid, new UpdateListener() {
                @Override
                public void done(BmobException e) {
                    if (e==null){
                        Toast.makeText(MenuActivity.this,"设置成功",Toast.LENGTH_SHORT).show();
                    }
                }
            });
        }

    }


    private void setBg() {
        menglong.setBackgroundColor(getResources().getColor(R.color.normal));
        bingxue.setBackgroundColor(getResources().getColor(R.color.normal));
        caise.setBackgroundColor(getResources().getColor(R.color.normal));
        huaduo.setBackgroundColor(getResources().getColor(R.color.normal));
        qunshang.setBackgroundColor(getResources().getColor(R.color.normal));
    }
    private void seticonBg(){
        boy.setBackgroundColor(getResources().getColor(R.color.normal));
        cat.setBackgroundColor(getResources().getColor(R.color.normal));
        girl.setBackgroundColor(getResources().getColor(R.color.normal));
        lufei.setBackgroundColor(getResources().getColor(R.color.normal));
        majcain.setBackgroundColor(getResources().getColor(R.color.normal));
        snowman.setBackgroundColor(getResources().getColor(R.color.normal));
    }

    private void sendBroadcastOnTheme(int theme) {
        Intent intent = new Intent(BROADCAST_MENUACTIVITY_CONTROL);
        intent.putExtra("theme", theme);
        sendBroadcast(intent);
    }
    private void sendBroadcastOnmode(int mode) {
        Intent intent = new Intent(BROADCAST_MENUMODE_CONTROL);
        intent.putExtra("mode", mode);
        sendBroadcast(intent);
    }

    //读取文件，返回String类型
    public String load(String data) {
        FileInputStream in = null;
        BufferedReader reader = null;
        StringBuilder content = new StringBuilder();
        try {
            in = openFileInput(data);           //“data”为文件名
            reader = new BufferedReader(new InputStreamReader(in));
            String line = "";
            while ((line = reader.readLine()) != null) {
                line= line + "\n";               //换行
                content.append(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return content.toString();
    }
    private void setmbg(int bg){
        switch (bg){
            case THEME_MENGLONG:
                menubg.setBackgroundResource(R.drawable.bg_blur);
                break;
            case THEME_BINGXUE:
                menubg.setBackgroundResource(R.drawable.bg_snow);
                break;
            case THEME_CAISE:
                menubg.setBackgroundResource(R.drawable.bg_color);
                break;
            case THEME_HUADUO:
                menubg.setBackgroundResource(R.drawable.bg_digit_flower);
                break;
            case THEME_QUNSHANG:
                menubg.setBackgroundResource(R.drawable.bg_mountain);
                break;

        }
    }
    private void seticonImg(String number){
        int in= Integer.parseInt(number);
        switch (in){
            case 10:
                iconimage.setImageResource(R.drawable.boy);
                break;
            case 11:
                iconimage.setImageResource(R.drawable.cat);
                break;
            case 12:
                iconimage.setImageResource(R.drawable.girl);
                break;
            case 13:
                iconimage.setImageResource(R.drawable.lufei);
                break;
            case 14:
                iconimage.setImageResource(R.drawable.majcain);
                break;
            case 15:
                iconimage.setImageResource(R.drawable.snowman);
                break;
        }
    }

}
